package com.aitaoxue.palx.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.LayoutParams;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;
import butterknife.ButterKnife;

import com.aitaoxue.palx.R;
import com.aitaoxue.palx.interf.BaseViewInterface;
import com.aitaoxue.palx.ui.dialog.CommonToast;
import com.aitaoxue.palx.ui.dialog.DialogControl;
import com.aitaoxue.palx.ui.dialog.DialogHelper;
import com.aitaoxue.palx.ui.dialog.WaitDialog;

public abstract class BaseActivity extends ActionBarActivity implements
		DialogControl, OnClickListener, BaseViewInterface {

	public static final String INTENT_ACTION_EXIT_APP = "INTENT_ACTION_EXIT_APP";

	private boolean _isVisiable;
	private WaitDialog _waitDialog;

	protected LayoutInflater mInflater;
	private ActionBar mActionBar;
	private TextView mTvActionTitle;

	protected View view = null;

	private BroadcastReceiver mExitReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context content, Intent intent) {
			finish();
		}

	};

	protected void onCreate(android.os.Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (!hasActionBar()) {
			supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
		}

		onBeforeSetContentLayout();
		if (getLayoutId() != 0) {
			setContentView(getLayoutId());
		}

		if (hasActionBar()) {
			mActionBar = getSupportActionBar();
		}

		mInflater = getLayoutInflater();

		if (hasActionBar()) {
			initActionBar(mActionBar);
		}
		init(savedInstanceState);

		IntentFilter filter = new IntentFilter(INTENT_ACTION_EXIT_APP);
		registerReceiver(mExitReceiver, filter);

		ButterKnife.inject(this);
		initView();
		initData();
		_isVisiable = true;
	}

	@Override
	protected void onDestroy() {
		unregisterReceiver(mExitReceiver);
		mExitReceiver = null;
		ButterKnife.reset(this);
		super.onDestroy();
	}

	protected void init(Bundle savedInstanceState) {
	}

	protected void onBeforeSetContentLayout() {
	}

	protected boolean hasActionBar() {
		return true;
	}

	protected int getLayoutId() {
		return 0;
	}

	protected boolean hasBackButton() {
		return false;
	}

	protected int getActionBarTitle() {
		return R.string.app_name;
	}

	protected int getActionBarCustomView() {
		return 0;
	}

	protected View inflateView(int resId) {
		return mInflater.inflate(resId, null);
	}

	protected void initActionBar(ActionBar actionBar) {
		if (actionBar == null)
			return;
		if (hasBackButton()) {
			actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
			int layoutRes = getActionBarCustomView();
			view = inflateView(layoutRes == 0 ? R.layout.actionbar_custom_backtitle
					: layoutRes);
			View back = view.findViewById(R.id.btn_back);
			if (back != null) {
				back.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						onBackPressed();
					}
				});
			}

			mTvActionTitle = (TextView) view
					.findViewById(R.id.tv_actionbar_title);
			if (mTvActionTitle != null) {
				int titleRes = getActionBarTitle();
				if (titleRes != 0) {
					mTvActionTitle.setText(titleRes);
				}
			}

			LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT);
			actionBar.setCustomView(view, params);
		} else {
			actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE);
			actionBar.setDisplayUseLogoEnabled(false);
			int titleRes = getActionBarTitle();
			if (titleRes != 0) {
				actionBar.setTitle(titleRes);
			}
		}
	}

	public void setActionBarTitle(int resId) {
		if (resId != 0) {
			setActionBarTitle(getString(resId));
		}
	}

	public void setActionBarTitle(String title) {
		if (hasActionBar()) {
			if (mTvActionTitle != null) {
				mTvActionTitle.setText(title);
			}
			mActionBar.setTitle(title);
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			onBackPressed();
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	public void showToast(int msgResid, int icon, int gravity) {
		showToast(getString(msgResid), icon, gravity);
	}

	public void showToast(String message, int icon, int gravity) {
		CommonToast toast = new CommonToast(this);
		toast.setMessage(message);
		toast.setMessageIc(icon);
		toast.setLayoutGravity(gravity);
		toast.show();
	}

	@Override
	public void hideWaitDialog() {
		if (_isVisiable && _waitDialog != null) {
			try {
				_waitDialog.dismiss();
				_waitDialog = null;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public WaitDialog showWaitDialog() {
		return showWaitDialog(R.string.loading);
	}

	@Override
	public WaitDialog showWaitDialog(int resId) {
		return showWaitDialog(getString(resId));
	}

	@Override
	public WaitDialog showWaitDialog(String text) {
		if (_isVisiable) {
			if (_waitDialog == null) {
				_waitDialog = DialogHelper.getWaitDialog(this, text);
			}
			if (_waitDialog != null) {
				_waitDialog.setMessage(text);
				_waitDialog.show();
			}

			return _waitDialog;
		}
		return null;
	}

}
